package dilmaj.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import dilmaj.client.AddAreasPanel.CloseHandler;
import dilmaj.shared.CommentVO;
import dilmaj.shared.TermVO;

public class SearchPanel extends PopupPanel {
	VerticalPanel mainPanel=new VerticalPanel();
	FlexTable resultsTable=new FlexTable();

	private ArrayList<TermVO> foundTerms=new ArrayList<TermVO>();
	
	private ArrayList<String> foundTitles=new ArrayList<String>();

	private DilmajConstants constants = GWT.create(DilmajConstants.class);
	Button closeButton=new Button(constants.close());
	
	TestGWT owner;
	
	public void setSearchTable(ArrayList<TermVO> terms, ArrayList<String> titles) {
		foundTerms.clear();
		foundTerms.addAll(terms);
		foundTitles.clear();
		foundTitles.addAll(titles);
		
	  int n=foundTerms.size();
	  //mainPanel.remove(resultsTable);
	  resultsTable.removeAllRows();
	  String caption="";
	  
	  for (int i=0;i<n;i++) {
		  int j=0;

		  TermVO termVO=terms.get(i);

		  resultsTable.setText(i, j++, termVO.getId()+""); //0
		  
		  Button button1=new Button("-");
		  button1.addClickHandler(owner.new MinusHandler());
		  button1.setTitle(foundTitles.get(i));
		  resultsTable.setWidget(i, j++, button1);//1
		  
		  Button button2=new Button("+");
		  button2.addClickHandler(owner.new PlusHandler());
		  button2.setTitle(foundTitles.get(i));
		  resultsTable.setWidget(i, j++, button2);//2
		  
		  resultsTable.setText(i, j++, termVO.getCaption());//3		
		  resultsTable.setText(i, j++, termVO.getSuggestion());//4
		  resultsTable.setText(i, j++, termVO.getRank()+"");//5
		  resultsTable.setText(i, j++, termVO.getUserName());//6
		  resultsTable.setText(i, j++, termVO.getAreas().toString());//7
		  
		  Button button8=new Button(constants.usecase());
		  button8.addClickHandler(owner.new UseCaseHandler(false));
		  button8.setTitle(foundTitles.get(i));
		  resultsTable.setWidget(i, j++, button8);//8
		  resultsTable.setText(i, j++, termVO.getPrivilege()+"");//9
		  
		  Button button10=new Button(constants.comments());
		  button10.addClickHandler(owner.new CommentsHandler());
		  button10.setTitle(foundTitles.get(i));
		  resultsTable.setWidget(i, j, button10);//10
	  }
	  //resultsTable=searchTable;
	}

	public SearchPanel(TestGWT caller, ArrayList<TermVO> terms, ArrayList<String> titles) {
		owner=caller;
		
		this.resultsTable=resultsTable;
		CloseHandler closeHandler=new CloseHandler(this);
		closeButton.addClickHandler(closeHandler);
		setSearchTable(terms, titles);
		mainPanel.add(resultsTable);
		mainPanel.add(closeButton);
		add(mainPanel);
	}
	
	class CloseHandler implements ClickHandler {
		private SearchPanel owner;
		
		public CloseHandler(SearchPanel owner) {
			this.owner=owner;
		}
		
		public void onClick(ClickEvent event) {
			owner.hide();
		}
	}
}
